/**
 * @author jingyong
 */

var form_cnt = 0;
var error_msg = "";
var active_id;

var mainOptions = { 
    success:       mainRes,  // post-submit callback
    dataType:      'json'
}; 

var atomOptions = {
    success:       atomRes,  // post-submit callback
    dataType:      'json'
};

// prepare the form when the DOM is ready 
$(document).ready(function() { 
     
    // bind to the form's submit event 
    $('#main-form').submit(function() { 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(mainOptions); 
 
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    }); 
}); 
 
// post-submit callback 
function mainRes(resData, statusText, xhr, $form)  { 
    if (resData.is_success){
        active_id = resData.active_id;
        var app_str = "<input type=\"hidden\" name=\"active_id\" value=\"" + active_id + "\" />"
        $(".atom-form").append(app_str);
        for (var i = 0; i < atom_cnt; i++)
            $(atom_form_selector[i]).ajaxSubmit(atomOptions);
    }
    else{
        alert(resData.errors);
    }
} 

function atomRes(resData, statusText, xhr, $form)  { 
    form_cnt++;
    if (!resData.is_success){
        error_msg += resData.errors;
    }
    
    if (form_cnt == atom_cnt){
        if (error_msg){
            alert(error_msg);
        }else{
            alert("成功！");
            window.location = "/content/active_detail/" + active_id + "/";
        }
    }
} 
